package com.xgkj.diyiketang.weidget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.GridView;
import android.widget.LinearLayout;

import com.xgkj.diyiketang.util.PalLog;
import com.xgkj.diyiketang.util.ViewUtil;

/**
 * Created by rhb on 2016/3/30.
 */
public class HorizontalGridView extends GridView{
    private final static String TAG = HorizontalGridView.class.getSimpleName();
    private float currentX;
    private float currentY;
    private float scrollX;
    private float scrollY;
    private float needX;
    private float needY;
    private boolean isFirst = false;
    private boolean isLeft = false;
    private Context mContext;
    private boolean isLanjie = false;
    private float lanjieX;
    private float lanjieY;

    public HorizontalGridView(Context context) {
        super(context);
        mContext = context;
    }

    public HorizontalGridView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
    }

    public HorizontalGridView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mContext = context;
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_MOVE){

            scrollX  = ev.getX();
            scrollY = ev.getY();
            needX = currentX-scrollX;
            needY = currentY - scrollY;

            if (!isFirst) {
                isFirst = true;
                if (Math.abs(needX) > Math.abs(needY) ){
                    isLeft = true;
                    this.getParent().requestDisallowInterceptTouchEvent(true);
                }else {
                    isLeft = false;
                }
            }

            if (isLeft){
//                PalLog.e(TAG,"当前位置："+currentX+"滚动位置："+scrollX+"需要滚动："+needX+"已经滚动："+getScrollX());
                currentX = scrollX;
                int parentWidth = ((LinearLayout) getParent()).getWidth();
                int left = getLeft();
                int width = parentWidth - left;
                if (getScrollX()+needX < 0 ){
                    scrollTo(0,0);
                }else if (getScrollX()+needX>getRight() - width){
                    scrollTo(getRight()-width,0);
                }else {
                    scrollBy((int) needX, 0);
                }
            }
        }else if (ev.getAction() == MotionEvent.ACTION_CANCEL){
            PalLog.e(TAG,"事件取消");
        }else if (ev.getAction() == MotionEvent.ACTION_UP){
            PalLog.e(TAG,"事件弹起");
            this.getParent().requestDisallowInterceptTouchEvent(false);
        }
        return true;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {

        if (ev.getAction() == MotionEvent.ACTION_DOWN){
            lanjieX = ev.getX();
            lanjieY = ev.getY();
            isLanjie = true;
        }

        if (ev.getAction() == MotionEvent.ACTION_UP || ev.getAction()==MotionEvent.ACTION_CANCEL){
            isLanjie = false;
        }
        if (ev.getAction() == MotionEvent.ACTION_MOVE){

            if (!isLanjie){
                lanjieX = ev.getX();
                lanjieY  = ev.getY();
                isLanjie = true;
            }else {
                currentX = ev.getX();
                currentY = ev.getY();
                needX = currentX-lanjieX;
                needY = currentY - lanjieY;
                if (Math.abs(needX) > 5 || Math.abs(needY)>5){
                    isFirst = false;
                    PalLog.e(TAG,"开始移动："+currentX+"Y:"+currentY+"lanjieX"+lanjieX+"lanjieY"+lanjieY);
//                    isLanjie = false;
                    return true;
                }
            }
        }
        return super.onInterceptTouchEvent(ev);
    }
}
